package net.jxvtc.blogv1.controller;


import net.jxvtc.blogv1.domain.User;
import net.jxvtc.blogv1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 *
 * @program: blogv1
 * @description: 用户主页控制器
 * @author: 蓝建平
 * @create: 2020-10-24 10:00
 **/
@Controller
@RequestMapping("/u")
public class UserspaceController {

	@Value("${file.server.url}")
	private String fileServerUrl;

	@Autowired
	private UserService userService;

	@Autowired
	private UserDetailsService userDetailsService;





 
	@GetMapping("/{username}")
	public String userSpace(@PathVariable("username") String username) {
		System.out.println("username" + username);
		return "/userspace/u";
	}
 
	@GetMapping("/{username}/blogs")
	public String listBlogsByOrder(@PathVariable("username") String username,
			@RequestParam(value="order",required=false,defaultValue="new") String order,
			@RequestParam(value="category",required=false ) Long category,
			@RequestParam(value="keyword",required=false ) String keyword) {
		
		if (category != null) {
			
			System.out.print("category:" +category );
			System.out.print("selflink:" + "redirect:/u/"+ username +"/blogs?category="+category);
			return "/userspace/u";
			
		} else if (keyword != null && keyword.isEmpty() == false) {
			
			System.out.print("keyword:" +keyword );
			System.out.print("selflink:" + "redirect:/u/"+ username +"/blogs?keyword="+keyword);
			return "/userspace/u";
		}  
		
		System.out.print("order:" +order);
		System.out.print("selflink:" + "redirect:/u/"+ username +"/blogs?order="+order);
		return "/userspace/u";
	}
	
	@GetMapping("/{username}/blogs/{id}")
	public String listBlogsByOrder(@PathVariable("id") Long id) {
		 
		System.out.print("blogId:" + id);
		return "/userspace/blog";
	}
	
	
	@GetMapping("/{username}/blogs/edit")
	public String editBlog(Model model) {
		model.addAttribute("fileServerUrl",fileServerUrl);
		return "/userspace/blogedit";
	}


	/**
			* 获取个人设置页面
	 *
			 * @param username
	 * @param model
	 * @return
			 */
	@GetMapping("/{username}/profile")
	public ModelAndView profile(@PathVariable("username") String username, Model model) {
		User user =  userService.getUser(username);
		model.addAttribute("user", user);
		model.addAttribute("fileServerUrl", fileServerUrl);// 文件服务器的地址返回给客户端
		return new ModelAndView("/userspace/profile", "userModel", model);
	}




}